package com.distribute.framework.core.web.helper;

import com.distribute.framework.core.web.helper.Utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ControllerHelper {
	
	
	/**
	 * 页面字段显示、编辑、查询、默认值策略(默认全部显示，可编辑，可列表,可查询)
	 */
	public static Boolean editable(Map controller,String key,Boolean defaultValue){
		String type = "editable";
		Boolean bAble = null;
		if(controller!=null){
			Boolean bDefaultEditable = getValue(controller,type,null);//取默认值
			Map fields = (Map) controller.get("fields");
			if(fields!=null){
				Map field = (Map) fields.get(key);
				bAble = getValue(field,type,bDefaultEditable);
			}else
				bAble = bDefaultEditable;
		}
		if(bAble!=null)
			return bAble;
		return defaultValue;
	}
	public static Boolean visible(Map controller,String key,Boolean defaultValue){		
		String type = "visible";
		Boolean bAble = null;
		if(controller!=null){
			Boolean bDefaultEditable = getValue(controller,type,null);//取默认值
			Map fields = (Map) controller.get("fields");
			if(fields!=null){
				Map field = (Map) fields.get(key);
				bAble = getValue(field,type,bDefaultEditable);
			}else
				bAble = bDefaultEditable;
		}
		if(bAble!=null)
			return bAble;
		return defaultValue;
		
	}
	public static Boolean queryable(Map controller,String key,Boolean defaultValue){		
		String type = "queryable";
		Boolean bAble = null;
		if(controller!=null){
			Boolean bDefaultEditable = getValue(controller,type,null);//取默认值
			Map fields = (Map) controller.get("fields");
			if(fields!=null){
				Map field = (Map) fields.get(key);
				bAble = getValue(field,type,bDefaultEditable);
			}else
				bAble = bDefaultEditable;
		}
		if(bAble!=null)
			return bAble;
		return defaultValue;		
	}
	public static Boolean listable(Map controller,String key,Boolean defaultValue){		
		String type = "listable";
		Boolean bAble = null;
		if(controller!=null){
			Boolean bDefaultEditable = getValue(controller,type,null);//取默认值
			Map fields = (Map) controller.get("fields");
			if(fields!=null){
				Map field = (Map) fields.get(key);
				bAble = getValue(field,type,bDefaultEditable);
			}else
				bAble = bDefaultEditable;
		}
		if(bAble!=null)
			return bAble;
		return defaultValue;		
	}
	public static Object permission(Map controller,String key){
		if(controller==null)
			controller=new HashMap();
		if(key.equals("canExp")){
			List canExp = (List) controller.get("canExp");
			if(canExp==null){
				canExp=new ArrayList();
				for(int i=canExp.size();i<13;i++)
					canExp.add(true);
			}
			return canExp;
		}
		return (Boolean) (controller.get(key)==null?true:controller.get(key));
	}
	public static Boolean get(Map controller,String key){
		if(controller==null)return false;
		return (Boolean) (controller.get(key)==null?true:controller.get(key));
	}
	public static <T> T getValue(Map map,String key,T defaultValue){
		if(map==null)return defaultValue;
		T value = (T) map.get(key);
		return value==null?defaultValue:value;
	}
	public static void executeControllerAfter(Map controller,Object object){
		if(controller==null)return;
		try{
			List<Map> functions = (List<Map>) controller.get("after");
			if(functions==null)return;
			for(Map map:functions){
				String className = (String) map.get("className");
				String methodName = (String) map.get("methodName");			
				Class<?> cls = Class.forName(className);
				Method method = cls.getMethod(methodName,Map.class,Object.class);
				method.invoke(null, controller,object);
			}
		}catch(Exception ex){
			throw new RuntimeException(ex);
		}
	}
	public static void executeControllerBefore(Map controller,Object object){
		if(controller==null)return;
		try{
			List<Map> functions = (List<Map>) controller.get("before");
			if(functions==null)return;
			for(Map map:functions){
				String className = (String) map.get("className");
				String methodName = (String) map.get("methodName");			
				Class<?> cls = Class.forName(className);
				Method method = cls.getMethod(methodName,Map.class,Object.class);
				method.invoke(null, controller,object);
			}
		}catch(Exception ex){
			throw new RuntimeException(ex);
		}
	}
	public static void setObjectDefaultValue(Object object,Map controller){
		if(controller==null)return;
		Map map = (Map) controller.get("fields");
		if(map==null)return;
		Field[] fields = object.getClass().getDeclaredFields();
		for(Field field:fields){
			try{
				field.setAccessible(true);	
				if(field.get(object)!=null)continue;
				Map fieldInfo = (Map) map.get(field.getName());
				if(fieldInfo==null)continue;
				if(fieldInfo.get("defaultValue")==null)continue;
				Class classType = getClassFromPrimitive(field.getType());
				Object objectValue = fieldInfo.get("defaultValue");
				if(objectValue==null)continue;
				String sObjectValue = objectValue.toString();
				if(Utils.empty(sObjectValue))continue;
				Object convertValue=null;
				if(classType.getSuperclass()==java.lang.Number.class){
					convertValue = classType.getMethod("valueOf", String.class).invoke(null,sObjectValue);
				}else if(classType==String.class){//java基本类型					
					convertValue = sObjectValue.toString();
				}else if(classType == java.util.Date.class){//日期类型					
					convertValue = new Date(Long.valueOf(sObjectValue));
				}
				field.set(object, convertValue);
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
	}
	/**
	 * 获取原始类对应的java类
	 * @param classType
	 * @return
	 */
	public static Class getClassFromPrimitive(Class classType){
		if(!classType.isPrimitive())return classType;
		String className = classType.getName();
		if(className.equals("byte"))return Byte.class;
		if(className.equals("short"))return Short.class;
		if(className.equals("int"))return Integer.class;
		if(className.equals("long"))return Long.class;
		if(className.equals("float"))return Float.class;
		if(className.equals("double"))return Double.class;	
		return null;
	}
	
	

}